//
//  JHRefreshAmazingAniView.m
//  JHRefresh
//
//  Created by Jiahai on 14-9-17.
//  Copyright (c) 2014年 Jiahai. All rights reserved.
//

#import "JHRefreshAmazingAniView.h"
#import "JHRefreshMacro.h"
#import "UIView+JHExtension.h"
#import "YMJNetWorking.h"
#define PINKCOLOR [UIColor colorWithRed:255 / 255.0 green:214 / 255.5 blue:213 / 255.0 alpha:100]
#define TEXTCOLOR [UIColor colorWithRed:237 / 255.0 green:95 / 255.5 blue:2 / 255.0 alpha:100]

@interface JHRefreshAmazingAniView ()
@property (nonatomic, assign) NSInteger dataArrayCount;
@property (nonatomic, assign) NSInteger alertCount;
@property (nonatomic, strong) NSArray *alertArray;
@end
@implementation JHRefreshAmazingAniView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _alertCount = 0;
        _alertArray = @[@"稍后再来,精彩继续",@"没有啦,去其他频道看看吧",@"亲,让我酝酿一下新内容",@"稍等片刻,马上就酝酿出来了",@"别着急待会儿给你好看的"];
        _aniImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:JHRefreshSrcName(@"walk0")]];
        _aniImgView.frame = CGRectMake(0, 0, 30, 38);
        _aniImgView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
        [self addSubview:_aniImgView];
        
        _statusLabel = [[UILabel alloc] init];
        _statusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _statusLabel.font = [UIFont boldSystemFontOfSize:11];
        _statusLabel.textColor = TEXTCOLOR;
        _statusLabel.backgroundColor = [UIColor clearColor];
        _statusLabel.textAlignment = NSTextAlignmentCenter;
        
        [self addSubview:_statusLabel];
        _statusLabel.text = @"";
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _statusLabel.frame = CGRectMake(0, 40, self.jh_width, 20);
    _aniImgView.center = CGPointMake(self.jh_width*0.5, self.jh_height*0.5);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

#pragma mark - JHRefreshViewDelegate

/**
 *  下拉时的动画
 */
- (void)refreshViewAniToBePulling
{
    switch (self.refreshViewType) {
        case JHRefreshViewTypeHeader:
        {
            _statusLabel.text = @"";
            
            
        }
            break;
        case JHRefreshViewTypeFooter:
        {
            _statusLabel.text = @"";
            
        }
            break;
    }
}
/**
 *  变成普通状态时的动画
 */
- (void)refreshViewAniToBeNormal
{
    [_aniImgView stopAnimating];
    _aniImgView.hidden = YES;
    switch (self.refreshViewType) {
        case JHRefreshViewTypeHeader:
        {
            
            if ([_statusLabel.text isEqualToString:@" "]){
                _statusLabel.text = @"亲,你的网络飞走了";
                _statusLabel.backgroundColor = PINKCOLOR;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    _statusLabel.text = @"";
                    _statusLabel.backgroundColor = [UIColor clearColor];
                    _aniImgView.hidden = NO;
                });
            } else if ([_statusLabel.text isEqualToString:@"  "]) {
                if (_dataArrayCount == 0) {
                    if (_alertCount == _alertArray.count) {
                        _alertCount = 0;
                    }
                    _statusLabel.text = _alertArray[_alertCount];
                    _statusLabel.backgroundColor = PINKCOLOR;
                    _alertCount++;
                    
                    
                } else {
                    _statusLabel.text = [NSString stringWithFormat:@"已为您智能推荐%ld条内容",_dataArrayCount];
                    _statusLabel.backgroundColor = PINKCOLOR;
                }
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    _statusLabel.text = @"";
                    _statusLabel.backgroundColor = [UIColor clearColor];
                    _aniImgView.hidden = NO;
                });
            }
            
            
            
        }
            break;
        case JHRefreshViewTypeFooter:
        {
            _statusLabel.text = @"";
            
            
        }
            break;
    }
}
/**
 *  刷新开始
 */
- (void)refreshViewBeginRefreshing
{
    if(!_aniImgView.animationImages)
    {
        NSMutableArray *arr = [NSMutableArray array];
        for (int i = 0; i < 7; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"walk%d",i]];
            [arr addObject:image];
        }
        _aniImgView.animationImages = arr;
        _aniImgView.animationDuration = arr.count / 12;
        _aniImgView.animationRepeatCount = 0;
    }
    
    //刷新开始时，设置aniImageView的宽高
//    _aniImgView.jh_width = _aniImgView.jh_height = JHRefreshViewHeight / 4 * 3;
    _aniImgView.jh_width = 30;
    _aniImgView.jh_height = 38;
    [_aniImgView startAnimating];
}
/**
 *  刷新结束
 *
 *  @param result 刷新结果
 */
/*传出刷新数组元素的个数*/
- (void)refreshViewEndRefreshing:(JHRefreshResult)result dataCount:(NSInteger)dataCount {
    [self refreshViewEndRefreshing:result];
    _dataArrayCount = dataCount;
    
}
- (void)refreshViewEndRefreshing:(JHRefreshResult)result
{
    switch (self.refreshViewType) {
        case JHRefreshViewTypeHeader:
        {
            switch (result) {
                case JHRefreshResultNone:
                    _statusLabel.text = @"";
                    break;
                case JHRefreshResultSuccess:
                {
                    _statusLabel.text = @"  ";
                    
                }
                    break;
                case JHRefreshResultFailure:
                {
                    _statusLabel.text = @" ";
                }
                    break;
            }
            
            
        }
            break;
        case JHRefreshViewTypeFooter:
        {
            _statusLabel.text = @"";
            
            
        }
            break;
    }
    [JHRefreshConfig updateLastUpdateTimeWithRefreshViewID:self.refreshViewID];
    
    
    
}
/**
 *  拖拽到对应的位置
 *
 *  @param pos 位置，范围：1 ~ JHRefreshViewHeight
 */
- (void)refreshViewPullingToPosition:(NSInteger)pos
{
    if(pos == 0)
    {
        return;
    }
    CGPoint center = _aniImgView.center;
    
//    _aniImgView.jh_width = _aniImgView.jh_height = pos / 4 * 3;
    _aniImgView.jh_width = 30;
    _aniImgView.jh_height = 38;
    
    _aniImgView.center = center;
    
//    NSString *name = [NSString stringWithFormat:@"dropdown_anim__000%d.png",(int)pos];
    NSString *name = [NSString stringWithFormat:@"walk%d",(int)(pos / 10 )];
    
    _aniImgView.image = [UIImage imageNamed:name];
    
}


@end
